package com.lagou.controller;

import com.lagou.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
public class PositionController {
    @Autowired
    private PositionService service;

    /**
     * 访问搜索页面
     * @return
     */
    @GetMapping({"/","index"})
    public String indexPage(){
        return "index";
    }

    /**
     * 读取MySQL数据并导入到ES中
     * @return
     */
    @RequestMapping("/importAll")
    @ResponseBody
    public String importAll(){
        try { service.importAll();
        } catch (IOException e) {
            e.printStackTrace(); }
        return "success";
    }

    /**
     * 分页查询ES数据
     * @param keyword 查询文本
     * @param pageNo 第几页
     * @param pageSize 每页大小
     * @return 查询ES返回结果集
     * @throws IOException IO异常
     */
    @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")
    @ResponseBody
    public List<Map<String,Object>> searchPosition(@PathVariable("keyword") String  keyword, @PathVariable("pageNo")int pageNo,
                                                   @PathVariable("pageSize")int  pageSize)throws  IOException{
        List<Map<String,Object>> list = service.searchPos(keyword, pageNo, pageSize);
        return  list;
    }

}
